package exportSystem.Formater;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;

import main.Main;

import org.apache.log4j.Logger;

import ui.flowManager.expirimentProperties.ExperimentProperties;

import exportSystem.Result.Result;

/**
 * Formater that returns result as CSV table
 * @author alon
 *
 */
public class CSVFormater implements IFormater{

	Logger log = Logger.getLogger(this.getClass());
	
	public CSVFormater(){
		
	}
	
	public void format(Result result,String name,double compAqurecy, double epsilonAqurecy) {
		File file;
		String str=new String();
		FileWriter fstream=null;
		BufferedWriter out;
		String dirName=System.getProperty(Main.EXPORT_DIR)+"Result\\"+ExperimentProperties.getExperimentName();
		if(!new File(dirName).exists()){
			boolean success = new File (dirName).mkdir();
			if(success){
				log.debug("created directory "+dirName);
				file=new File(dirName,"agent"+name+"_log_"+System.currentTimeMillis()+".csv");	
			}else{
				log.debug("directory not created writing to relative path");
				file=new File("agent"+name+"_log_"+System.currentTimeMillis()+".csv");	
			}
		}else{	
			log.debug(dirName+" exists write to it");
			file=new File(dirName,"agent"+name+"_log_"+System.currentTimeMillis()+".csv");	
		}
		try {
			fstream = new FileWriter(file);
		} catch (IOException e) {
			log.debug("failed to init file stream\n"+ e.getMessage());
		}
		out = new BufferedWriter(fstream);
		log.debug("starting export result to csv");
		ArrayList<ArrayList<Double>> data=result.getMetric();
		Set<String> keys=result.getMetaData().getMetaData().keySet();
		str+=createHeader(keys,result,compAqurecy,epsilonAqurecy);
		for(int i=0;i<data.size();i++){
			ArrayList<Double> row=data.get(i);
			//use meta data to render each row
			int currentIndex=0;
			for(String key:keys){
				for(int t=0;t<result.getMetaData().getMetaData().get(key);t++){
					str+=row.get(currentIndex++)+",";
				}
			str+="\t,";	
			}
			str+="\n";
		}
		try{
			// Create file 
			out.write(str);
			out.close();
			log.debug("done writing a buffer");
		}catch (Exception e){//Catch exception if any
			log.debug("writing log failed\n"+ e.getMessage());
		}
	}
	
	/**
	 * creates a header for the CSV
	 * @param keys Keys
	 * @param result the result
	 * @param compAqurecy competence Accuracy 
	 * @param epsilonAqurecy epsilon Accuracy
	 * @return
	 */
	private String createHeader(Set<String> keys,Result result,double compAqurecy,double epsilonAqurecy){
		String string="";
		string="Competence accuracy: "+compAqurecy+"\n";
		string+="Epsilon accuracy: "+epsilonAqurecy+"\n";
		for(String key:keys){
			for(int t=0;t<result.getMetaData().getMetaData().get(key);t++){
				if(t==0){
					string+=key+",";
				}else{
					string+=",";
				}
			}
			string+="\t,";	
		}
		string+="\n";
		for(String key:keys){
			for(int t=0;t<result.getMetaData().getMetaData().get(key);t++){
				string+="agent"+(t+1)+",";
			}
			string+="\t,";	
		}
		string+="\n";
		return string;
	}
	
}
